﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using WF_Business;

namespace Business.FlowOperation
{
    /// <summary>
    /// st_instance表操作类
    /// </summary>
    public class InstanceOperation
    {
        public string AllCount(string strAppend)
        {
            string strSql = "select count(*) from st_instance a where 1=1 and a.isdelete <>1";
            strSql += strAppend;
            return SysParams.OAConnection().GetValue(strSql);
        }

        public string Processing(string strAppend)
        {
            string strSql = "select count(*) from st_instance a where status<>'2' and a.isdelete <>1";
            strSql += strAppend;
            return SysParams.OAConnection().GetValue(strSql);
        }

        public string HaveProcessed(string strAppend)
        {
            string strSql = "select count(*) from st_instance a where status='2' and a.isdelete <>1";
            strSql += strAppend;
            return SysParams.OAConnection().GetValue(strSql);
        }

        public string Stop(string strAppend)
        {
            string strSql = "select count(*) from st_instance a where status='-2' and a.isdelete <>1";
            strSql += strAppend;
            return SysParams.OAConnection().GetValue(strSql);
        }

        public string GetStatusById(string id)
        {
            string s_GDstate;
            string tempsql = string.Format("select status from st_instance where iid='{0}' and isdelete <>1", id);
            string strStatus = SysParams.OAConnection().GetValue(tempsql);

            if (string.IsNullOrEmpty(strStatus))
            {
                s_GDstate = "未办结";
            }
            else
            {
                if (strStatus.Equals("2") || strStatus.Equals("3"))
                {
                    s_GDstate = "办结";//归档状态
                }
                else
                {
                    s_GDstate = "未办结";
                }
            }
            return s_GDstate;
        }

        /// <summary>
        /// 获取优先级分配
        /// </summary>
        /// <param name="strBusinessType">业务类型</param>
        /// <param name="strUnitName">单位名称</param>
        /// <param name="strStartDate">开始时间</param>
        /// <param name="strEndDate">结束时间</param>
        /// <returns></returns>
        public DataTable GetPriority(string strBusinessType, string strUnitName, string strStartDate, string strEndDate)
        {
            string strSql = string.Format(@"select Rownum as num,f.wname,i.Name,to_char(i.accepted_time,'yyyy-mm-dd hh24:mi:ss') accptime,i.iid,i.priority  
                                    from st_instance i,st_workflow f Where i.wid=f.wid and i.status='1' ");
            if (!string.IsNullOrEmpty(strUnitName))
                strSql += string.Format(" and i.name like '%{0}%'", strUnitName);
            if (!string.IsNullOrEmpty(strStartDate))
                strSql += string.Format(" and i.ACCEPTED_TIME>=to_date('{0}','yyyy-mm-dd')", strStartDate);
            if (!string.IsNullOrEmpty(strEndDate))
                strSql += string.Format(" and i.ACCEPTED_TIME<to_date('{0}','yyyy-mm-dd')+1", strEndDate);
            if (!string.IsNullOrEmpty(strBusinessType))
                strSql += string.Format(" and f.wname = '{0}'", strBusinessType);

            DataTable dtTemp = new DataTable();
            SysParams.OAConnection().RunSql(strSql, out dtTemp);
            return dtTemp;
        }

        public DataTable GetInfo(string userid, string struserno, string strusername, bool ISsystemUser)
        {
            
            string strSql = "select a.iid,a.name from st_instance a,st_workflow b where 1=1 and a.wid=b.wid and a.isdelete <>1";

            if (!string.IsNullOrEmpty(struserno))
            {
                strSql += (" and iid like '%" + struserno + "%'");
            }

            if (!string.IsNullOrEmpty(strusername))
            {
                strSql += (" and name like '%" + strusername + "%'");
            }

            if (!ISsystemUser)//不是系统管理员
            {
                strSql += @" and b.wname in 
                                (select t.task  from xt_query_right t  where t.userid = '" + userid + "')";
            }

            DataTable dtOut;
            SysParams.OAConnection().RunSql(strSql, out dtOut);
            return dtOut;
        }

        /// <summary>
        /// 获取案例信息
        /// </summary>
        /// <param name="strIid">案件编号</param>
        /// <param name="strName">申请人姓名</param>
        /// <param name="strWid">流程id</param>
        /// <returns></returns>
        public DataTable GetSerialInfo(string strIid, string strName, string strWid)
        {
            string strSql = "select a.iid,a.name from st_instance a where isdelete <>1";
            if (!string.IsNullOrEmpty(strIid))
            {
                strSql += string.Format(" and iid like '%{0}%'", strIid);
            }
            if (!string.IsNullOrEmpty(strName))
            {
                strSql += string.Format(" and name like '%{0}%'", strName);
            }
            if (!string.IsNullOrEmpty(strWid))
            {
                strSql += string.Format(" and a.wid='{0}'", strWid);
            }

            DataTable dtOut;
            SysParams.OAConnection().RunSql(strSql, out dtOut);
            return dtOut;
        }

        /// <summary>
        /// 根据iid获取wid
        /// </summary>
        /// <param name="strWid">The STR wid.</param>
        /// <returns></returns>
        public string GetMaxIidByWid(string strWid)
        {
            string strSql = string.Format("select max(iid) from st_instance a where wid='{0}'", strWid);

            return SysParams.OAConnection().GetValue(strSql);
        }
    }
}
